import { Component, OnInit, ViewChild } from "@angular/core";
import { NzMessageService } from "ng-zorro-antd/message";
import { DictionaryItemService } from "./dictionary-item.service";
import { ActivatedRoute } from "@angular/router";
import { DictionaryItemEditComponent } from "./dictionary-item-edit/dictionary-item-edit.component";

@Component({
  selector: "app-dictionary-item",
  templateUrl: "./dictionary-item.component.html",
  styles: []
})
export class DictionaryItemComponent implements OnInit {

  checked = false;
  indeterminate = false;

  listOfCurrentPageData = [];
  setOfCheckedId = new Set<number>();

  loading = false;
  // 字典主表ID
  dictId : string;

  // 设备组数据
  dataList = [];

  // 查询参数
  searchBeans = [{
    attributeName: "label",
    searchType: "LIKE",
    attributeValue: ""
  }];
  // 分页参数
  pageData = { page: 0, size: 10, total: 100 };

  @ViewChild("dictionaryItemEditComponent", { static: false })
  public dictionaryItemEditComponent: DictionaryItemEditComponent;

  constructor(private dictionaryItemService: DictionaryItemService,
              private activateInfo: ActivatedRoute,
              private msgSrv: NzMessageService) {

    activateInfo.params.subscribe(params => {

        this.dictId = params.dictId;
    });
  }

  ngOnInit(): void {
    this.loadListData();
  }

  delete(id) {
    this.loading = true;
    this.dictionaryItemService.delete(id).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.msgSrv.success("删除完成");
      } else {
        this.msgSrv.error(res.message);
      }
    });
    console.log(id);


  }

  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = "";
    });
  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success("已取消");
  }

  openAdd() {
    this.dictionaryItemEditComponent.open(this.dictId);
  }

  openEdit(record): void {

    console.log(record);
    this.dictionaryItemEditComponent.edit(record,this.dictId);

  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

     let search = this.searchBeans.concat({
      attributeName: "dictionary.id",
      searchType: "EQ",
      attributeValue: this.dictId
    });

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: search
      }
    };

    this.dictionaryItemService.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.pageData.page = res.data.number;
        this.pageData.size = res.data.size;
        this.pageData.total = res.data.totalElements;
        this.dataList = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  onAllChecked(checked: boolean): void {
    this.listOfCurrentPageData.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateCheckedSet(id, checked));
    this.refreshCheckedStatus();
  }

  updateCheckedSet(id: number, checked: boolean): void {
    if (checked) {
      this.setOfCheckedId.add(id);
    } else {
      this.setOfCheckedId.delete(id);
    }
  }

  refreshCheckedStatus(): void {
    const listOfEnabledData = this.listOfCurrentPageData.filter(({ disabled }) => !disabled);
    this.checked = listOfEnabledData.every(({ id }) => this.setOfCheckedId.has(id));
    this.indeterminate = listOfEnabledData.some(({ id }) => this.setOfCheckedId.has(id)) && !this.checked;
  }

  onItemChecked(id: number, checked: boolean): void {
    this.updateCheckedSet(id, checked);
    this.refreshCheckedStatus();
  }

  _onReuseInit() {
    this.loadListData();
  }
}
